package org.whatisjava.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;

public class MemoryAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    private InputStream imageStream;

    public InputStream getImageStream() {
        return this.imageStream;
    }

    public void setImageStream(InputStream imageStream) {
        this.imageStream = imageStream;
    }

    /**
     * Ajax测试方法
     * 
     * @return
     */
    public String testAjax() {
        return "success";
    }

    /**
     * 图片测试
     * 
     * @return
     */
    public String testImage() throws Exception {
        String fileName = "/WEB-INF/test.jpg";
        fileName = application.getRealPath(fileName);
        System.out.println("路径是:" + fileName);
        try {
            imageStream = new FileInputStream(fileName);
        } catch (Exception e) {
        }
        return "success";
    }

    /**
     * 
     * @return
     */
    public String execute() {
        System.out.println("execute...");
        return "success";
    }

    /**
     * 
     * @return
     */
    public String go() {

        System.out.println(this + "go...");

        // Actioncontext提供了很多供Action使用东西,每一次请求的Action都有一个ActionContext
        ActionContext ctx = ActionContext.getContext();

        System.out.println(ctx);

        // 值堆栈,可以看成是一个数据空间,类型是ValueStack类型的,就相当于在Ognl的基础之上进行了进一步 的封装
        // 每一次请求都会创建一个ValueStack对象
        ValueStack stack = ctx.getValueStack();

        /**
         * 拦截器INTERCEPTOR将请求封装成,<br>
         * 有一个拦截器的功能是获得当前表单提交的数据<br>
         * 通过OGNL的方式将起封装填充在 ValueStack里面
         * 
         */

        // 该类型就是一个集合,superClass ---> ArrayList
        CompoundRoot root = stack.getRoot();

        System.out.println("------Compound<组合的,复合的>Root-----");
        for (int i = 0; i < root.size(); i++) {
            System.out.println(root.get(i));
        }
        System.out.println("------CompoundRoot-----");

        stack.setValue("emp.name", "Java");
        stack.setValue("emp.salary", new BigDecimal("20000"));

        System.out.println(stack.findValue("emp.name"));
        return "success";
    }
}
